2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Input;
7 namespace SuperPolarity
9 static class InputController
11 static Dictionary<string, List<Action<float>>> Listeners;
12 static Dictionary<string, List<Keys>> RegisteredKeys;
13 static Dictionary<string, List<Buttons>> RegisteredButtons;
14 static List<string> BlockedKeys;
15 static List<string> BlockedButtons;
17 static GamePadState InputGamePadState;
18 static KeyboardState InputKeyboardState;
23 * You register: name of the event (ie. attack) and a key associated with it.
24 * or button... Left Stick /always/ dispatches move event.
27 static InputController()
29 Listeners = new Dictionary<string,List<Action<float>>>();
30 RegisteredButtons = new Dictionary<string, List<Buttons>>();
31 RegisteredKeys = new Dictionary<string, List<Keys>>();
32 BlockedKeys = new List<string>();
33 BlockedButtons = new List<string>();
34 InputKeyboardState = new KeyboardState();
35 InputGamePadState = new GamePadState();
38 public static void UpdateInput()
42 DispatchRegisteredEvents();
45 private static void Poll()
47 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
48 InputKeyboardState = Keyboard.GetState();
51 public static void RegisterEventForKey(string eventName, Keys key)
53 List<Keys> newKeyList;
54 if (!RegisteredKeys.ContainsKey(eventName))
56 newKeyList = new List<Keys>();
57 RegisteredKeys.Add(eventName, newKeyList);
60 RegisteredKeys.TryGetValue(eventName, out newKeyList);
65 public static void RegisterEventForButton(string eventName, Buttons button)
67 List<Buttons> newButtonList;
68 if (!RegisteredButtons.ContainsKey(eventName))
70 newButtonList = new List<Buttons>();
71 RegisteredButtons.Add(eventName, newButtonList);
74 RegisteredButtons.TryGetValue(eventName, out newButtonList);
76 newButtonList.Add(button);
79 private static void DispatchRegisteredEvents()
83 foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
85 foreach (Keys key in entry.Value)
87 if (InputKeyboardState.IsKeyDown(key))
89 if (!BlockedKeys.Contains(entry.Key))
91 Dispatch(entry.Key, 1);
92 BlockedKeys.Add(entry.Key);
101 BlockedKeys.Remove(entry.Key);
105 foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
108 foreach (Buttons button in entry.Value)
110 if (InputGamePadState.IsButtonDown(button))
112 if (!BlockedButtons.Contains(entry.Key))
114 Dispatch(entry.Key, 1);
115 BlockedButtons.Add(entry.Key);
124 BlockedButtons.Remove(entry.Key);
129 private static void DispatchMoveEvents()
131 float xMovement = 0.0f;
132 float yMovement = 0.0f;
133 // Dispatch the moveX / MoveY events every frame.
135 xMovement = InputGamePadState.ThumbSticks.Left.X;
136 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
138 if (InputKeyboardState.IsKeyDown(Keys.Left))
143 if (InputKeyboardState.IsKeyDown(Keys.Right))
148 if (InputKeyboardState.IsKeyDown(Keys.Up))
153 if (InputKeyboardState.IsKeyDown(Keys.Down))
158 Dispatch("moveX", xMovement);
159 Dispatch("moveY", yMovement);
162 public static void Bind(string eventName, Action<float> listener)
164 List<Action<float>> newListenerList;
165 List<Action<float>> listenerList;
168 if (!Listeners.ContainsKey(eventName)) {
169 newListenerList = new List<Action<float>>();
170 Listeners.Add(eventName, newListenerList);
173 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
175 listenerList.Add(listener);
178 public static void Dispatch(string eventName, float value)
180 List<Action<float>> listenerList;
183 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
190 foreach (Action<float> method in listenerList)